package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import org.springframework.util.ObjectUtils;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listTree() {
        List<CategoryEntity> list = list();
        //获取一级分类
        List<CategoryEntity> entityList = list.stream().filter(item -> item.getParentCid() == 0).map(item -> {
            item.setChildren(getChildList(item, list));
            return item;
        }).sorted(Comparator.comparingInt(o -> (o.getSort() == null ? 0 : o.getSort()))).collect(Collectors.toList());
        return entityList;
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId, paths);
        Collections.reverse(parentPath);
        return parentPath.toArray(new Long[parentPath.size()]);
    }

    @Override
    public void updateCategoryById(CategoryEntity category) {
        updateById(category);
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setCatelogId(category.getCatId());
        entity.setCatelogName(category.getName());
        categoryBrandRelationService.update(Wrappers.<CategoryBrandRelationEntity>lambdaUpdate().setEntity(entity));
    }

    private List<Long> findParentPath(Long catelogId,List<Long> paths){
        paths.add(catelogId);
        CategoryEntity category = getById(catelogId);
        if (category.getParentCid() != 0){
            findParentPath(category.getParentCid(),paths);
        }
        return paths;
    }

    private List<CategoryEntity> getChildList(CategoryEntity item, List<CategoryEntity> list) {
        return list.stream()
                .filter(e -> e.getParentCid().equals(item.getCatId()))
                .peek(s -> {
                    List<CategoryEntity> childList = getChildList(s, list);
                    if (ObjectUtils.isEmpty(childList)){
                        s.setChildren(null);
                    }else {
                        s.setChildren(childList);
                    }
                })
                .sorted(Comparator.comparingInt(o -> (o.getSort() == null ? 0 : o.getSort())))
                .collect(Collectors.toList());
    }
}